package com.hsnn.medstgmini.pro.project.controller;

import java.lang.reflect.Type;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.base.std.model.StdCompgroup;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.pro.project.model.BidProjCompany;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.model.form.BidProjCompanyForm;
import com.hsnn.medstgmini.pro.project.service.BidProjCompanyManager;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/bidProjCompany")
public class BidProjCompanyController {
    private static final Logger log = Logger.getLogger(BidProjCompanyController.class);
	
	@Autowired
	private BidProjCompanyManager bidProjCompanyManager;

	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 
	 *@category 跳向已添加配送企业列表
	 *@author 张俊
	 *@date 2015年6月12日 下午15:54:04
	 *@return
	 */
	@RequestMapping("/toList")
	public String toList(){
			
		request.setAttribute("projId", request.getParameter("projId"));
		request.setAttribute("projName", request.getParameter("newname"));
		return "/pro/project/bidProjCompanyList";
	}
	
	/**
	 * @category 查询已添加配送企业列表
	 * @author 张俊
	 * @date 2015-06-25 09:30:46
	 * @param result
	 * @param bidProjCompany
	 * @param stdCompinfo
	 * @return
	 */
	@RequestMapping("/getDataList")
	@ResponseBody
	public Pagination getBidProjCompanyList(BidProjCompany bidProjCompany, StdCompinfo stdCompinfo, StdCompgroup stdCompgroup, BidProject bidProject) {
		Pagination page = new Pagination(request);
		try {
			List<BidProjCompanyForm> list = bidProjCompanyManager.getBidProjCompanyList(page, bidProjCompany, stdCompinfo, stdCompgroup, bidProject);
			page.setRows(list);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 启用|停用
	 * @param dicPhar
	 * @return
	 */
	@RequestMapping("/changeStatus")
	@ResponseBody
	public Pagination changeStatus(BidProjCompany bidProjCompany) {
		Pagination page = new Pagination(request);
		try {
			if (bidProjCompany != null && bidProjCompany.getProjDelId() != null) {
				bidProjCompany.setUpdUser("zhangj");
				bidProjCompanyManager.changeStatus(bidProjCompany);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 批量(启用|停用)
	 * @param array
	 * @return
	 */
	@RequestMapping("/batchChangeStatus")
	@ResponseBody
	public Pagination batchChangeStatus(String array) {
		Pagination page = new Pagination(request);
		try {
			if (StringUtils.isNotEmpty(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<BidProjCompany>>() {
				}.getType();
				List<BidProjCompany> list = gson.fromJson(array, type);
				if (list != null && !list.isEmpty()) {
					for (BidProjCompany bpc : list) {
						bpc.setUpdUser("zhangj");
					}
					bidProjCompanyManager.batchChangeStatus(list);
					page.setSuccess(true);
				}
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 跳转到配送企业列表页面(中心)
	 * @author 韩守松
	 * @date   2015年6月15日
	 * @param  @return
	 */
	@RequestMapping(value="/toListJsp",method={RequestMethod.GET,RequestMethod.POST})
	public String toDicListJsp(){
		return "/pro/project/bidProjCompanyCheckList";
	}
	
	/**
	 * 配送企业数据获取
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/getDicDataList")
	@ResponseBody
	public Pagination getDicDataList() {
		Pagination page = new Pagination(request);
		page.getConditions().put("projId", request.getParameter("projId"));
		page.getConditions().put("compName", request.getParameter("compName"));
		page.getConditions().put("compCode", request.getParameter("compCode"));
		page.setRows(bidProjCompanyManager.getAllBidProjCompanyList(page));
		page.setSuccess(true);
		return page;
	}	
}
